package com.example.apijson.controller;

import apijson.framework.APIJSONController;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * APIJSON控制器
 * 提供GET、HEAD、GETS、HEADS、POST、PUT、DELETE等接口
 */
@RestController
@RequestMapping("")
public class MyAPIJSONController extends APIJSONController {

    private static final Logger logger = LoggerFactory.getLogger(MyAPIJSONController.class);

    /**
     * 普通获取数据
     */
    @PostMapping("get")
    @Override
    public String get(@RequestBody String request, HttpSession session) {
        logger.debug("收到GET请求: {}", request);
        try {
            return super.get(request, session);
        } catch (Exception e) {
            logger.error("处理GET请求出错", e);
            return "{\"code\":500,\"msg\":\"" + e.getMessage() + "\"}";
        }
    }

    /**
     * 普通获取数量
     */
    @PostMapping("head")
    @Override
    public String head(@RequestBody String request, HttpSession session) {
        logger.debug("收到HEAD请求: {}", request);
        try {
            return super.head(request, session);
        } catch (Exception e) {
            logger.error("处理HEAD请求出错", e);
            return "{\"code\":500,\"msg\":\"" + e.getMessage() + "\"}";
        }
    }


    /**
     * 安全/私密获取数据
     */
    @PostMapping("gets")
    @Override
    public String gets(@RequestBody String request, HttpSession session) {
        logger.debug("收到GETS请求: {}", request);
        try {
            return super.gets(request, session);
        } catch (Exception e) {
            logger.error("处理GETS请求出错", e);
            return "{\"code\":500,\"msg\":\"" + e.getMessage() + "\"}";
        }
    }

    /**
     * 安全/私密获取数量
     */
    @PostMapping("heads")
    @Override
    public String heads(@RequestBody String request, HttpSession session) {
        logger.debug("收到HEADS请求: {}", request);
        try {
            return super.heads(request, session);
        } catch (Exception e) {
            logger.error("处理HEADS请求出错", e);
            return "{\"code\":500,\"msg\":\"" + e.getMessage() + "\"}";
        }
    }


    /**
     * 新增数据
     */
    @PostMapping("post")
    @Override
    public String post(@RequestBody String request, HttpSession session) {
        logger.debug("收到POST请求: {}", request);
        try {
            return super.post(request, session);
        } catch (Exception e) {
            logger.error("处理POST请求出错", e);
            return "{\"code\":500,\"msg\":\"" + e.getMessage() + "\"}";
        }
    }

    /**
     * 修改数据
     */
    @PostMapping("put")
    @Override
    public String put(@RequestBody String request, HttpSession session) {
        logger.debug("收到PUT请求: {}", request);
        try {
            return super.put(request, session);
        } catch (Exception e) {
            logger.error("处理PUT请求出错", e);
            return "{\"code\":500,\"msg\":\"" + e.getMessage() + "\"}";
        }
    }


    /**
     * 删除数据
     */
    @PostMapping("delete")
    @Override
    public String delete(@RequestBody String request, HttpSession session) {
        logger.debug("收到DELETE请求: {}", request);
        try {
            return super.delete(request, session);
        } catch (Exception e) {
            logger.error("处理DELETE请求出错", e);
            return "{\"code\":500,\"msg\":\"" + e.getMessage() + "\"}";
        }
    }
}